package com.yxy.controller.app;

import com.yxy.common.Const;
import com.yxy.common.ServerResponse;
import com.yxy.controller.service.SportDynamicController;
import com.yxy.dto.SportDynamicModel;
import com.yxy.entity.SportDynamic;
import com.yxy.service.IFileService;
import com.yxy.service.SportDynamicService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/app/sportDynamic")
public class AppSportDynamicController {

    public Logger logger = LoggerFactory.getLogger(AppSportDynamicController.class);


    @Autowired
    private SportDynamicService sportDynamicService;
    @Autowired
    private IFileService fileService;

    //列表
    @RequestMapping(value = "list", method = RequestMethod.GET)
    @ResponseBody
    public ServerResponse<List<SportDynamicModel>> list(Integer pageNum,Integer pageSize) throws Exception {
        List<SportDynamicModel> sportDynamicModels = new ArrayList<SportDynamicModel>();
        List<SportDynamic> sportDynamicList = sportDynamicService.findPage(pageNum,pageSize);
        if (sportDynamicList != null && sportDynamicList.size() > 0) {
            for (SportDynamic sportDynamic : sportDynamicList) {
                SportDynamicModel sportDynamicModel = new SportDynamicController().entityToModel(sportDynamic);
                sportDynamicModels.add(sportDynamicModel);
            }
        }
        return ServerResponse.createBySuccess("查询成功", sportDynamicModels);
    }

    //添加
    @RequestMapping(value = "create")
    @ResponseBody
    public ServerResponse<SportDynamicModel> cerate(@RequestParam(value = "file", required = false) MultipartFile file, String sportDynamicContent,Integer userId,HttpServletRequest request) {

        SportDynamic sportDynamic = new SportDynamic();
        sportDynamic.setSportDynamicContent(sportDynamicContent);
        sportDynamic.setUserId(userId);
        String path = request.getSession().getServletContext().getRealPath("upload");
        String targetFileName = fileService.uploadFace(file, path, Const.IMG_SPORT_DYNAMIC);
        if (targetFileName == null) {
            return ServerResponse.createByErrorCodeMessage(Const.UPLOAD_FIED, "图片上传失败");
        }
        sportDynamic.setSportDynamicImg(targetFileName);
        setTemplateDateAndUser(sportDynamic, request);
        if (sportDynamicService.create(sportDynamic)) {
            return ServerResponse.createBySuccessMessage("动态发布成功");
        }

        return ServerResponse.createByErrorMessage("发布失败,请重试");
    }

    //删除
    @RequestMapping("delete")
    @ResponseBody
    public ServerResponse delete(Integer id, HttpServletRequest request) throws Exception {
        SportDynamic sportDynamic = new SportDynamic();
        sportDynamic.setId(id);
        setTemplateDateAndUser(sportDynamic, request);
        if (sportDynamicService.delete(sportDynamic)) {
            return ServerResponse.createBySuccessMessage("动态删除成功");
        }
        return ServerResponse.createByErrorMessage("删除失败，请重试！");
    }
    @RequestMapping("doLike")
    @ResponseBody
    public ServerResponse doLike(Integer userId,Integer sportDynamicId) throws Exception{
        if(sportDynamicService.SportDynamicDoLike(userId,sportDynamicId)){
            return ServerResponse.createBySuccessMessage("操作成功");
        }
        return ServerResponse.createByErrorMessage("操作失败");
    }
    @RequestMapping("selectMySportDynamic")
    @ResponseBody
    public ServerResponse<List<SportDynamicModel>> selectMySportDynamic(Integer userId) throws Exception{
        List<SportDynamic> sportDynamicList = sportDynamicService.selectMySportDynamic(userId);
        if (sportDynamicList!=null && sportDynamicList.size()>0){
            List<SportDynamicModel> sportDynamicModelList = new ArrayList<>();
            for (SportDynamic sportDynamic:sportDynamicList) {
                SportDynamicModel sportDynamicModel = new SportDynamicController().entityToModel(sportDynamic);
                sportDynamicModelList.add(sportDynamicModel);
            }
            return ServerResponse.createBySuccess("成功获取我发布的活动",sportDynamicModelList);
        }
        return ServerResponse.createBySuccess("暂无动态发布数据",null);
    }

    /**
     * 统一设置 Date
     *
     * @param entity
     */
    public void setTemplateDateAndUser(SportDynamic entity, HttpServletRequest request) {

        if (entity == null) {
            logger.error("ERR:Date set error.");
            return;
        }
        if (entity.getId() == null && entity.getCrteateDatetime() == null) {
            //如果id为空， 就是新建
            entity.setCrteateDatetime(new Date());

        } else {

            if (entity.getUpdateDatetime() == null) {
                entity.setUpdateDatetime(new Date());
            }
        }
    }
}
